﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class admin_ManageJobs : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_JOBS = "__JOBS::JOBS";
    private const string PROP_LAYOUT = "__JOBS::UIL";
    private const string PROP_SELECTED_JOB_ID = "__JOBS::SJOBID";

    #endregion

    #region Public Properties

    public JobCollection DataSourceJobs
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBS] is JobCollection ? (JobCollection)SessionMgr.Session[PROP_DATASRC_JOBS] : (JobCollection)(SessionMgr.Session[PROP_DATASRC_JOBS] = new JobCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBS] = value; }
    }

    public string SelectedJobID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_JOB_ID] is string ? (string)SessionMgr.Session[PROP_SELECTED_JOB_ID] : null); }
        set { SessionMgr.Session[PROP_SELECTED_JOB_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceJobs = Job.GetJobByJobIsActive(false);
            NonActiveJobsList1.DataBind(DataSourceJobs);
            DataSourceJobs = Job.GetJobByJobIsActive(true);
            ActiveJobsList1.DataBind(DataSourceJobs);
        }
    }

    #endregion

    #region Control: NonActiveJobsList

    protected void ucNonActiveJobsList_EditRequested(object sender, Controls_NonActiveJobsList.EditRequestedEventArgs evt)
    {
        SelectedJobID = evt.J.JobID;
        JobsViewEdit1.DataBind(evt.J);
        SetLayout(LayoutType.Edit);
    }


    protected void ucNonActiveJobsList_AddNewRequested(object sender, EventArgs evt)
    {
        JobsViewEdit1.CancelEdit();
        Job newJob = Job.CreateNew(DataSourceJobs);
        SelectedJobID= newJob.JobID;
        //ConsultantViewEdit1.DataBind(DataSourceConsultants.FindByConsultantID(SelectedCONSULTANTID));
        SetLayout(LayoutType.Edit);
    }


    protected void ucNonActiveJobsList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceJobs = Job.GetJobByJobIsActive(false);
        NonActiveJobsList1.DataSource = DataSourceJobs;
    }

    #endregion

    #region Control: ActiveJobsList

    protected void ucActiveJobsList_EditRequested(object sender, Controls_ActiveJobsList.EditRequestedEventArgs evt)
    {
        SelectedJobID = evt.J.JobID;
        JobsViewEdit1.DataBind(evt.J);
        SetLayout(LayoutType.Edit);
    }


    protected void ucActiveJobsList_AddNewRequested(object sender, EventArgs evt)
    {
        JobsViewEdit1.CancelEdit();
        Job newJob = Job.CreateNew(DataSourceJobs);
        SelectedJobID = newJob.JobID;
        //ConsultantViewEdit1.DataBind(DataSourceConsultants.FindByConsultantID(SelectedCONSULTANTID));
        SetLayout(LayoutType.Edit);
    }


    protected void ucActiveJobsList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceJobs = Job.GetJobByJobIsActive(true);
        ActiveJobsList1.DataSource = DataSourceJobs;
    }

    #endregion

    #region Control: JobsViewEdit

    protected void ucJobsViewEdit_DataSourceConsultantRequested(object sender, EventArgs evt)
    {
        JobsViewEdit1.DataSourceJob = DataSourceJobs.FindByJobID(SelectedJobID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        JobsViewEdit1.CancelEdit();
        // If we were adding a new Job...then remove the incomplete/not added new Job object from the collection.
        if (DataSourceJobs.FindByJobID(SelectedJobID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceJobs.Remove(DataSourceJobs.FindByJobID(SelectedJobID));
                SelectedJobID = (DataSourceJobs[0] != null ? DataSourceJobs[0].JobID: null);
            }
            catch
            {
            }
        }
        DataSourceJobs = Job.GetJobByJobIsActive(false);
        NonActiveJobsList1.DataBind(DataSourceJobs);
        DataSourceJobs = Job.GetJobByJobIsActive(true);
        ActiveJobsList1.DataBind(DataSourceJobs);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (JobsViewEdit1.EndEdit())
        {
            Job obj = DataSourceJobs.FindByJobID(SelectedJobID);
            
            Job.Update(obj);
            //Set the feedback text here.....
            DataSourceJobs = Job.GetJobByJobIsActive(false);
            NonActiveJobsList1.DataBind(DataSourceJobs);
            DataSourceJobs = Job.GetJobByJobIsActive(true);
            ActiveJobsList1.DataBind(DataSourceJobs);
            Response.AddHeader("Refresh", "0");
            SetLayout(LayoutType.View);
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlNonActiveJobs.Visible = false;
                pnlActiveJobs.Visible = false;
                pnlJobsViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlNonActiveJobs.Visible = true;
                pnlActiveJobs.Visible = true;
                pnlJobsViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion
}
